package com.example.demo.commons.core.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * <p> 正则表达式 </p>
 *
 * @author tanyunpeng
 * @date 2021/11/18
 */
public final class LocalRexValidUtils {

    public final static Pattern PHONE_PATTERN_STRING = Pattern.compile("^1[3456789]\\d{9}$");
    public final static Pattern LIKE_PHONE_NUMBER = Pattern.compile("^\\d{1,11}$");
    public final static Pattern DIGIT_PATTERN = Pattern.compile("^\\d+$");
    public final static Pattern HAS_CHINESE_PATTERN = Pattern.compile("[\\u4e00-\\u9fa5]");
    public static final Pattern ENGLISH_VAR = Pattern.compile("^[A-Za-z0-9]+$");

    public static boolean geneMatcher(Pattern pattern, String str) {
        boolean flag;
        try {
            Matcher matcher = pattern.matcher(str);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    public static boolean isEnglishVar(String var) {
        return geneMatcher(ENGLISH_VAR, var);
    }

    /**
     * 验证手机号码
     */
    public static boolean isPhone(String mobiles) {
        return geneMatcher(PHONE_PATTERN_STRING, mobiles);
    }

    /**
     * 验证是否像手机号码
     */
    public static boolean isLikeMobile(String mobiles) {
        return geneMatcher(LIKE_PHONE_NUMBER, mobiles);
    }

    /**
     * 是否为数字
     */
    public static boolean isDigit(String digit) {
        return geneMatcher(DIGIT_PATTERN, digit);
    }

    /**
     * 是否中文
     */
    public static boolean hasChinese(String str) {
        boolean flag;
        try {
            Matcher matcher = HAS_CHINESE_PATTERN.matcher(str);
            if (matcher.find()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}
